package com.kaoyaya.delivery.adapter;

import java.util.List;

import com.kaoyaya.delivery.activity.R;
import com.kaoyaya.delivery.data.entity.DeliveryRecord;
import com.kaoyaya.delivery.data.entity.OrderItem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 发货记录
 * @author yubin
 * @data 2013-5-25
 */

public class DeliveryRecordAdapter extends BaseExpandableListAdapter {
	private Context context;
	private LayoutInflater inflater;
	private List<DeliveryRecord> deliveryRecords;
	private View.OnClickListener clickListener;

	public DeliveryRecordAdapter(Context context, List<DeliveryRecord> deliveryRecords, View.OnClickListener clickListener) {
		this.context = context;
		this.deliveryRecords = deliveryRecords;
		inflater = LayoutInflater.from(context);
		this.clickListener = clickListener;
	}
	
	public List<DeliveryRecord> getRecords() {
		return deliveryRecords;
	}
	
	public void addRecord(List<DeliveryRecord> records) {
		this.deliveryRecords.addAll(records);
	}

	@Override
	public OrderItem getChild(int groupPosition, int childPosition) {
		return deliveryRecords.get(groupPosition).getOrder().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolderChild child;
		OrderItem orderItem = getChild(groupPosition, childPosition);
		if (convertView == null) {
			child = new ViewHolderChild();
			convertView = inflater.inflate(R.layout.item_del_record_child, null);
			child.tvName = (TextView) convertView.findViewById(R.id.tvName);
			child.tvArea = (TextView) convertView.findViewById(R.id.tvArea);
			child.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
			child.tvGold = (TextView) convertView.findViewById(R.id.tvGold);
			child.tvType = (TextView) convertView.findViewById(R.id.tvType);
			child.btnRefund = (Button) convertView.findViewById(R.id.btnRefund);
			child.btnRefund.setTag(orderItem);
			convertView.setTag(child);
		} else {
			child = (ViewHolderChild) convertView.getTag();
		}
		child.tvName.setText(orderItem.getName());
		child.tvArea.setText(orderItem.getArea());
		child.tvPrice.setText(String.valueOf(orderItem.getPrice()));
		child.tvGold.setText(String.valueOf(orderItem.getGold()));
		child.tvType.setText(orderItem.getType() == 0 ? "单品" : "套餐");

		if (deliveryRecords.get(groupPosition).getOrder().get(childPosition).getStatus() == 0) {
			child.btnRefund.setText("退款");
			child.btnRefund.setEnabled(true);
			child.btnRefund.setOnClickListener(clickListener);
		} else {
			if (deliveryRecords.get(groupPosition).getOrder().get(childPosition).getStatus() == 1) {
				child.btnRefund.setText("已退款");
			} else {
				child.btnRefund.setText("退款截止");
			}
			child.btnRefund.setEnabled(false);
		}
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return deliveryRecords.get(groupPosition).getOrder().size();
	}

	@Override
	public DeliveryRecord getGroup(int groupPosition) {
		return deliveryRecords.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return deliveryRecords.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		ViewHolderGroup group;
		DeliveryRecord deliveryRecord = getGroup(groupPosition);
		if (convertView == null) {
			group = new ViewHolderGroup();
			convertView = inflater.inflate(R.layout.item_del_record_group, null);
			group.tvCardTime = (TextView) convertView.findViewById(R.id.tvCardTime);
			group.tvUsername = (TextView) convertView.findViewById(R.id.tvUsername);
			group.tvRealname = (TextView) convertView.findViewById(R.id.tvRealname);
			group.tvMobile = (TextView) convertView.findViewById(R.id.tvMobile);
			group.tvEmail = (TextView) convertView.findViewById(R.id.tvEmail);
			group.ivPlusMinus = (ImageView) convertView.findViewById(R.id.ivPlusMinus);
			group.btnRedelivery = (Button) convertView.findViewById(R.id.btnRedelivery);
			group.btnRedelivery.setTag(deliveryRecord);
			convertView.setTag(group);
		} else {
			group = (ViewHolderGroup) convertView.getTag();
		}
		if (isExpanded) {
			group.ivPlusMinus.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_minus_black));
		} else {
			group.ivPlusMinus.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_plus_black));
		}
		group.tvCardTime.setText(deliveryRecords.get(groupPosition).getCardTime().toLocaleString());
		group.tvUsername.setText(deliveryRecords.get(groupPosition).getUsername());
		group.tvRealname.setText(deliveryRecords.get(groupPosition).getRealName());
		group.tvMobile.setText(deliveryRecords.get(groupPosition).getMobile());
		group.tvEmail.setText(deliveryRecords.get(groupPosition).getEmail());
		group.btnRedelivery.setOnClickListener(clickListener);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	private class ViewHolderGroup {
		TextView tvCardTime, tvUsername, tvRealname, tvMobile, tvEmail;
		Button btnRedelivery;
		ImageView ivPlusMinus;
	}

	private class ViewHolderChild {
		TextView tvName, tvType, tvArea, tvPrice, tvGold;
		Button btnRefund;
	}
}